
//init state
const state = () => ({
  param: [
    /*{
      orderId: '210807179610847412',
      orderStatus: "已完成",
      paymentTime: "2021-8-5",
      orderComMsgId: 2,
      postage: 0,
      total: 1000,
      getAdress: 1,
      paymentWay: 1,
      userId: 1
    },
    {
      orderId: '210807179610847413',
      orderStatus: "待付款",
      paymentTime: "2021-8-5",
      orderComMsgId: 3,
      postage: 0,
      total: 1000,
      getAdress: 1,
      paymentWay: 1,
      userId: 1
    },
    {
      orderId: '210807179610847414',
      orderStatus: "已取消",
      paymentTime: "2021-8-5",
      orderComMsgId: 4,
      postage: 0,
      total: 1000,
      getAdress: 1,
      paymentWay: 1,
      userId: 1
    },*/
  ]
})

//getters
const getters = {
  getDataByOrderId(state){/*通过订单编号查询订单信息*/
    return function (orderId) {
      return state.param.filter(item => {
        return item.orderId == orderId;
      })[0];
    }
  },
  getAllOrderMsgAndOrderComMsg(state,getter){//获取所有信息用于显示订单信息列表
    let tempVal = [];
    state.param.filter(item => {
      tempVal.push(getter["getManyDataByOrderId"](item.orderId));
    });
    return tempVal;
  },
  getManyDataByOrderId(state,getter,rootState,rootGetter){
    return function (orderId) {
      let orderMsg = getter["getDataByOrderId"](orderId);//取出订单信息
      let manyComMsg = rootGetter["OrderComMsg/getManyComDataByOrderId"](orderId);//取出商品信息
      console.log(orderMsg);
      console.log(manyComMsg);
      let adsMsg = rootGetter["ShippingAddress/getDataById"](orderMsg.getAdress);//取出收货地址信息
      orderMsg = {...orderMsg,...{comMsg: manyComMsg},...adsMsg};
      return orderMsg;
    }
  },
  refreshOrderSessionStorageData(state){//session的刷新方法
    sessionStorage.setItem("Order",JSON.stringify(state.param));
  }
}

//actions
const actions = {
  loadDataForOrder({commit},payload){//加载数据
    commit("loadDataForOrderMut",payload);
  }
}

//mutations
const mutations = {
  loadDataForOrderMut(state,val){//加载数据
    state.param = [...val];
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
